package com.huang.forkjoin;


import java.util.concurrent.RecursiveTask;

/*
  求和计算
* */
public class ForkJoinDemo extends RecursiveTask<Long> {
       private long start;
       private long end;
       private long temp=10000l;
       long sum=0;

    public ForkJoinDemo(long start, long end) {
        this.start=start;
        this.end=end;
    }

    @Override
    protected Long compute() {
            if((end-start)<temp){
                for (long i = start; i < end; i++) {
                        sum+=i;
                }
                return sum;
            }else{
                long middle=(end-start)/2;
                ForkJoinDemo t1 = new ForkJoinDemo(start, middle);
                t1.fork();  //拆分任务  把任务亚茹线程队列
                ForkJoinDemo t2= new ForkJoinDemo(middle+1, end);
                t2.fork();
                return t1.join()+t2.join();

            }

    }
}
